// pages/user/share.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath:'',
    user:{},
    gameCard: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:JSON.parse(options.user),
    })
    //this.createCommentHB();
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  createCommentHB: function (e) {
    var that = this;
    var token = wx.getStorageSync('token');
    if (token) {


      var context = wx.createCanvasContext('mycanvas');
      context.drawImage(this.data.gameCard.poster, 0, 0, 751, 1061);//绘制首图
      context.save()
      context.beginPath()
      context.arc(378, 252, 88, 0, 2 * Math.PI)
      context.setFillStyle('#EEEEEE')
      context.clip()
      context.drawImage(this.data.gameCard.avatar, 290, 164, 176, 176);//画logo
      context.restore()
      context.save()
      context.beginPath()
      // context.drawImage(this.data.gameCard.headwear, 253, 133, 240, 240);//画logo
      context.draw();
      //将生成好的图片保存到本地，需要延迟一会，绘制期间耗时
      setTimeout(function () {
        wx.canvasToTempFilePath({
          canvasId: 'mycanvas',
          success: function (res) {
            var tempFilePath = res.tempFilePath;
            that.setData({
              imagePath: tempFilePath,
              maskHidden: true,
            });
            wx.hideLoading();
            console.log("海报图片路径：" + res.tempFilePath);
            that.savePosterImage(that.data.imagePath);
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }, 1500);





    }
    else {
      wx.navigateTo({
        url: '/pages/auth/getuserinfo',
      })
    }

  },
  downloadIMG: function () {
    wx.showLoading({
      title: "正在生成海报",
      mask: true,
    });
    this.getImageInfo(this.data.user.poster, 'poster');
    this.getImageInfo(this.data.user.avatar, 'avatar');
    this.getImageInfo(this.data.user.headwear, 'headwear');
    setTimeout(()=>{
      this.createCommentHB();
    },2000)
   
    
  },
  savePosterImage: function (img) {
    wx.request({
      url: app.globalData.api + '/users/poster.html&token=' + wx.getStorageSync('token'),
      method: 'post',
      data: {
        "grade": this.data.user.grade

      },
      success: (res) => {

      }
    })
    var _this=this;
    wx.saveImageToPhotosAlbum({
      filePath: img,
      success(result) {
        console.log(result)
        wx.showModal({
          title: '提示',
          content: '海报已存入手机相册，赶快分享到朋友圈吧',
          showCancel: false,
          success: (res) => {

          }
        })
      },
      fail: function (err) {
        console.log(err);
        if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          console.log("再次发起授权");
          wx.showModal({
            title: '用户未授权',
            content: '如需保存海报图片到相册，需获取授权.是否在授权管理中选中“保存到相册”?',
            showCancel: true,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.openSetting({
                  success: function success(res) {
                    console.log('打开设置', res.authSetting);
                    wx.openSetting({
                      success(settingdata) {
                        console.log(settingdata)
                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                          console.log('获取保存到相册权限成功');
                        }
                        else {
                          console.log('获取保存到相册权限失败');
                        }
                      }
                    })

                  }
                });
              }

            }
          })

        }



      }
    });
  },  
  getImageInfo(url, type) {    //  图片缓存本地的方法
    if (typeof url === 'string') {
      wx.getImageInfo({   //  小程序获取图片信息API
        src: url,
        success: (res) => {
          if (type == "avatar") {
            this.data.gameCard.avatar = res.path;
          }
          if (type == "poster") {
            this.data.gameCard.poster = res.path;
          }
          if (type =="headwear"){
            this.data.gameCard.headwear = res.path;
          }
          this.setData({
            gameCard: this.data.gameCard
          })
        },
        fail(err) {
          console.log(err)
        }
      })
    }
  }  
})